﻿///<reference path="_moduleReferences.ts" />

module ScrambledEggs.Modules {
    export class Play implements IModule {

        private _onUpdate: () => void;
        private _standingUp: bool = false;
        private _grew: bool = false;

        constructor() {
        }

        initialize(gameState: GameStateProvider, onUpdate: () => void ) {
            var me = this;
            var play = $("#play");
            me._onUpdate = onUpdate;

            play.find(".poop").click(function () {
                gameState.State.Fullness = 0;
                var success = 50 + Math.random() * 150 < (gameState.State.MentalMaturity + gameState.State.PhysicalMaturity) / 2;
                if (success) {
                    gameState.State.Message = "You did it!!";
                    gameState.State.MentalMaturity += 15;
                    gameState.State.PhysicalMaturity += 15;
                }
                else {
                    gameState.State.Message = "A heroic attempt! Unfortunately, you pooped your pants instead! =(";
                    gameState.State.MentalMaturity += 2;
                    gameState.State.PhysicalMaturity += 2;
                }
                me._onUpdate();
            });

            play.find(".rattle").click(function () {
                gameState.State.Message = "You shake your rattle in a cute fashion";
                gameState.State.PhysicalMaturity += 5;
                gameState.State.Fullness = Utility.Number.Limit(gameState.State.Fullness - 20, 0, 100);
                Cooldowns.SetIfGreater("#play button", 5);
                me._onUpdate();
            });

            var creations = ["a house", "the eiffel tower", "a bridge", "a cake", "a cat"];
            play.find(".blocks").click(function () {
                gameState.State.Message = "You made <em>" + creations[Math.floor(Math.random() * creations.length)] + "</em> out of blocks.";
                gameState.State.MentalMaturity += 6;
                gameState.State.Fullness = Utility.Number.Limit(gameState.State.Fullness - 20, 0, 100);
                Cooldowns.SetIfGreater("#play button", 5);
                me._onUpdate();
            });

            play.find(".crawl").click(function () {
                if (gameState.State.LeftPen) {
                    Cooldowns.Lock();
                    gameState.State.Message = "You crawl towards what looks like The Cat's <i>=°ᴥ°=</i> Food Bowl. <i>(O)</i>";
                    me._onUpdate();
                    var crawling = setInterval(function () {
                        gameState.State.Fullness -= 3;
                        gameState.State.PhysicalMaturity ++;
                        if (gameState.State.Fullness <= 0) {
                            if (gameState.State.PhysicalMaturity >= 120) {
                                gameState.State.Message = "You reach the Food Bowl <i>(O)</i> of The Cat <i>=°ᴥ°=</i>";
                                gameState.State.DecisionCatFood = CatFoodDecision.Reached;
                            } else
                                gameState.State.Message = "It's just a bit further...";
                            gameState.State.Fullness = 0;
                            clearInterval(crawling);
                            Cooldowns.Unlock();
                        }
                        me._onUpdate();
                    }, 400);
                    return;
                }

                gameState.State.Message = "Crawling around";
                Cooldowns.Lock();
                var duration = Math.floor(Math.random() * 5 + 4);
                var interval = setInterval(function () {
                    if (--duration <= 0 || (gameState.State.Fullness -= 8) <= 0) {
                        if (gameState.State.Fullness < 0)
                            gameState.State.Fullness = 0;

                        if (gameState.State.Inventory["PenKey"] == 1 && !gameState.State.LeftPen) {
                            gameState.State.Message = "<em>While crawling around, you managed to open your playing pen!</em>";
                            gameState.State.LeftPen = true;
                        }
                        else {
                            gameState.State.Inventory["PenKey"] = 0;
                            gameState.State.Message = (Math.floor(Math.random() * 1000) % 2) ? "You bump your head and decide to stop crawling." : "Phew! You got bored of crawling.";
                        }
                        clearInterval(interval);
                        Cooldowns.Unlock();
                        me._onUpdate();
                        return;
                    }
                    gameState.State.PhysicalMaturity += 2;
                    gameState.State.Message += ".";
                    me._onUpdate();
                }, 500);
            });

            play.find(".lookaround").click(function () {
                if (gameState.State.DecisionCatFood == CatFoodDecision.Reached) {
                    gameState.State.Message = "The Cat <i>=°ᴥ°=</i> looks at you suspiciously. What will you do with the Food Bowl <i>(O)</i>?";
                    me._onUpdate();
                    return;
                }
                if (gameState.State.LeftPen) {
                    gameState.State.Message = "Looking around...";
                    me._onUpdate();
                    var looking = setInterval(function () {
                        gameState.State.Fullness -= 3;
                        gameState.State.MentalMaturity++;
                        if (gameState.State.Fullness <= 0) {
                            gameState.State.Fullness = 0;
                            gameState.State.Message = "In the distance, you can see the Food Bowl <i>(O)</i> of The Cat <i>=°ᴥ°=</i>";
                            clearInterval(looking);
                        }
                        me._onUpdate();
                    }, 400);
                    return;
                }

                gameState.State.Message = "Looking around";
                Cooldowns.Lock();
                var duration = Math.floor(Math.random() * 5 + 4);

                var interval = setInterval(function () {
                    if (--duration <= 0 || (gameState.State.Fullness -= 8) <= 0) {
                        if (gameState.State.Fullness < 0)
                            gameState.State.Fullness = 0;

                        if (gameState.State.MentalMaturity >= 70 && !gameState.State.Inventory["PenKey"]) {
                            gameState.State.Message = "<em>While looking around, you found an interesting object.</em>";
                            gameState.State.Inventory["PenKey"] = -1;
                        }
                        else {
                            gameState.State.Message = (Math.floor(Math.random() * 1000) % 2) ? "You find nothing of interest" : "The way The Cat <i>=°ᴥ°=</i> is staring at you makes you feel uneasy.";
                        }
                        clearInterval(interval);
                        Cooldowns.Unlock();
                        me._onUpdate();
                        return;
                    }

                    gameState.State.MentalMaturity += 2;
                    gameState.State.Message += ".";
                    me._onUpdate();
                }, 900);
            });

            play.find(".stand").click(function () {
                gameState.State.Message = "If only you could reach the door knob <i>--o</i>...";
                if (gameState.State.Temporary["statsToDistribute"] == null)
                    gameState.State.Temporary["statsToDistribute"] = 130;
                me._onUpdate();

                var tryGetUp = function (callback) {
                    gameState.State.Message = "You're trying to get up...";
                    gameState.State.MentalMaturity += 4;
                    gameState.State.PhysicalMaturity += 4;
                    gameState.State.Fullness -= 4;
                    gameState.State.Temporary["statsToDistribute"] -= 4;
                    me._onUpdate();

                    setTimeout(function () {
                        callback();
                    }, 250);
                };
                var searchFood = function (callback) {
                    gameState.State.Message = "You get hungry and grab around for food..";
                    me._onUpdate();
                    setTimeout(function () {
                        if (gameState.State.DecisionCatFood == CatFoodDecision.Ate)
                            gameState.State.Message = "There are still some leftovers in the Food Bowl <i>(O)</i>";
                        else
                            gameState.State.Message = "You think about how good the food in the Food Bowl <i>(O)</i> looked. Somehow, you find the strength to go on.";
                        gameState.State.Fullness += 60;
                        me._onUpdate();
                        setTimeout(function () {
                            callback();
                        }, 3000);
                    }, gameState.State.DecisionCatFood == CatFoodDecision.Ate ? 2000 : 6000);

                };
                var keepTrying = function () {
                    if (gameState.State.Temporary["statsToDistribute"] <= 0) {
                        gameState.State.Temporary["statsToDistribute"] = 0;
                        gameState.State.Message = "<strong>Somehow, you managed to get up!</strong>";
                        me._standingUp = true;
                        me._onUpdate();
                        Cooldowns.Unlock();
                        return;
                    }
                    if (gameState.State.Fullness < 4)
                        searchFood(function () { keepTrying(); });
                    else
                        tryGetUp(function () { keepTrying(); });
                };
                Cooldowns.Lock();
                setTimeout(keepTrying, 2000);
            });

            play.find(".handle").click(function () {
                if (!$(this).is(".clickable"))
                    return;
                if (me._grew) {
                    $(this).removeClass("clickable");
                    gameState.State.Message = "<em>You reached the door knob! <i>--o</i></em> ...";
                    setTimeout(function () {
                        gameState.State.Message = "You reached the door knob! <i>--o</i> But it's <em>stuck</em>!";
                        me._onUpdate();
                    }, 2000);
                    setTimeout(function () {
                        gameState.State.Message = "You reached the door knob! <i>--o</i> But it's <em>stuck</em>! <strong>Damn you</strong>, baby-protected door knob!";
                        me._onUpdate();
                    }, 4000);
                    setTimeout(function () {
                        gameState.State.Message = "The Cat <i>=°ᴥ°=</i> the seems amused...";
                        me._onUpdate();
                        play.find(".doorcat").fadeIn(2000);
                        $("#babyfinale button").not(".extra, .continue").fadeIn(4000);
                        if (gameState.State.MentalMaturity >= 450 && gameState.State.PhysicalMaturity >= 450) {
                            $("#babyfinale button.extra").fadeIn(4000);
                        }
                    }, 6000);
                }
                else
                    gameState.State.Message = "You're still having trouble reaching the door knob. <i>--o</i> ";
                me._onUpdate();
            });
            play.find(".hand").click(function () {
                gameState.State.Message = "You reach out <em>really</em> hard...";
                $(".hand").html("/").removeClass("clickable");
                play.find(".legs").addClass("clickable");
                me._onUpdate();
            });
            play.find(".legs").click(function () {
                if (!$(this).is(".clickable"))
                    return;
                $(this).removeClass("clickable");
                gameState.State.Message = "You reach out <em>REALLY REALLY</em> hard...";
                play.find(".longlegs").slideDown("slow");
                me._grew = true;
                me._onUpdate();
            });
            play.find(".doorcat").click(function () {
                console.warn("can't touch this!");
            });
            play.find(".finale button").click(function () {
                delete gameState.State.Temporary["statsToDistribute"];
                switch ($(this).attr("class")) {
                    case "diapercat":
                        play.find(".catstains").show();
                        gameState.State.Message = "The Cat <i>=°ᴥ°=</i> yelps <strong>LOUDLY</strong> at this offence!";
                        gameState.State.DecisionBabyFinale = BabyFinaleDecision.Poopcat;
                        break;
                    case "bangdoor":
                        play.find(".doorsound").show();
                        gameState.State.Message = "You bang the door <strong>LOUDLY</strong> in your fury.";
                        gameState.State.DecisionBabyFinale = BabyFinaleDecision.Doorbang;
                        break;
                    case "settledown":
                        play.find(".doorsound").show();
                        gameState.State.Message = "You settle down and pet The Cat <i>=°ᴥ°=</i>.";
                        gameState.State.DecisionBabyFinale = BabyFinaleDecision.Petcat;
                        break;
                    case "extra":
                        play.find(".door").hide();
                        gameState.State.Message = "The Cat <i>=°ᴥ°=</i> is impressed by your skill. <strong>Let it be known that you are one of the few who make their own path.</strong>";
                        gameState.State.DecisionBabyFinale = BabyFinaleDecision.Jailbreak;
                        break;
                    default:
                        console.warn("dafuq did you do?!");
                }
                me._onUpdate();
            });
            play.find(".continue").click(function () {
                switch (gameState.State.DecisionBabyFinale) {
                    default:
                        console.warn("dafuq did you do?!");
                }
            });
        }

        update(gameState: GameStateProvider, triggerSource: IModule): void {
            var me = this;
            var play = $("#play");
            if (gameState.State.Phase != GamePhase.Baby || gameState.State.Fullness < 0) {
                play.hide();
                return;
            }
            play.show();

            play.find(".rattle").toggle(gameState.State.Fullness >= 20 && gameState.State.Inventory["PenKey"] != -1 && !gameState.State.LeftPen);
            play.find(".blocks").toggle(gameState.State.Fullness >= 20 && gameState.State.Inventory["PenKey"] != -1 && !gameState.State.LeftPen);
            play.find(".crawl").toggle(gameState.State.Fullness >= 30 && gameState.State.MentalMaturity > 40 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood < CatFoodDecision.Reached);
            play.find(".lookaround").toggle(gameState.State.Fullness >= 30 && gameState.State.PhysicalMaturity > 40 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood < CatFoodDecision.Ate);
            play.find(".poop").toggle(gameState.State.Fullness >= 70 && gameState.State.Inventory["PenKey"] != -1 && gameState.State.DecisionCatFood != CatFoodDecision.Reached && !me._standingUp);
            play.find(".stand").toggle(gameState.State.DecisionCatFood >= CatFoodDecision.Ate && !me._standingUp);
            play.find(".opendoor").toggle(me._standingUp);
            play.find(".continue").toggle(gameState.State.DecisionBabyFinale != BabyFinaleDecision.None);

            if (triggerSource != this && gameState.State.Fullness >= 100) {
                gameState.State.Fullness = 0;                
                gameState.State.Message = "You ate too much and pooped your pants...";
                me._onUpdate();
            }
        }
    }
}